package com.example.springbootsfzy.service.gengminghui;

import com.example.springbootsfzy.domain.Courses;
import com.example.springbootsfzy.mapper.CoursesMapper;
import com.example.springbootsfzy.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {

    @Autowired
    private CoursesMapper coursesMapper;

//查询所有课程信息
    public List<Courses> getAllCourses() {
        return coursesMapper.selectAll();
    }
//通过id查询课程信息
    public Courses getCourseById(Integer id) {
        return coursesMapper.selectByPrimaryKey(id);
    }
//通过id删除课程信息
    public R deleteByPrimaryKey(Integer id) {
        if (coursesMapper.deleteByPrimaryKey(id) > 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }
//添加课程信息
    public R insert(Courses courses) {
        if (coursesMapper.insert(courses) > 0) {
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }
//修改课程信息
    public R updateById(Courses courses) {
        if (coursesMapper.updateByPrimaryKeySelective(courses) > 0) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }}

    //分页查询
    public List<Courses> selectByCoursePage(int pageNum, int pageSize) {
        int start = (pageNum - 1) * pageSize;
        return coursesMapper.selectByCoursePage(start, pageSize);
    }

    public PageInfo<Courses> selectByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Courses> list = coursesMapper.selectAll();
        PageInfo<Courses> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //连表查询所有信息
    public List<Courses> selectAllInfo(int pageNum, int pageSize) {
        int start = (pageNum - 1) * pageSize;
        return coursesMapper.selectAllInfoPage(start, pageSize);

    }
    public PageInfo<Courses> selectAllInfoPage(int pageNum, int pageSize) {
      PageHelper.startPage(pageNum, pageSize);
      List<Courses> list = coursesMapper.selectAllInfo1();
      PageInfo<Courses> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //通过课程id查询所有信息
    public List<Courses> selectAllInfoPageByCOurseId(Integer courseId) {
        return coursesMapper.selectAllInfoPageByCourseId(courseId);
    }

    //通过咨询师id查询课程信息
    public List<Courses> selectAllInfoByconsultantId(Integer consultantId) {
        return coursesMapper.selectAllInfoByconsultantId(consultantId);
    }

    //模糊搜索
    public List<Courses> selectByLike(String courseName,Integer consultantId) {
        return coursesMapper.selectAllInfoLike(courseName,consultantId);
    }

}
